package com.ctshk.app.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.ctshk.common.constant.RedisConstants;
import com.ctshk.common.enums.LoginType;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.RSAUtil;
import com.ctshk.common.web.support.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *
 * </p>
 *
 * @author 李新科
 * @date 2021/6/3 12:54
 */
@Component
public class AppBaseController extends BaseController {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Value("${auth.user.token.timeout.app}")
    private Long tokenTimeoutApp;

    @Value("${auth.user.token.timeout.sapp}")
    private Long tokenTimeoutsApp;

    @Override
    public TokenUser getCurrentUser(HttpServletRequest request) {
        String token = request.getHeader(RedisConstants.JWT_TOKEN_HEADER);
        if (StringUtils.isBlank(token)) {
            throw new BusinessException(SystemError.USER_1032);
        }
        TokenUser tokenUser = new TokenUser();
        try {
            token = token.replace(RedisConstants.JWT_TOKEN_PREFIX, Strings.EMPTY);
            String tokenDecrypt = RSAUtil.decryptStr(token);
            tokenUser = JSONObject.parseObject(tokenDecrypt, TokenUser.class);
        } catch (Exception e) {
            throw new BusinessException(SystemError.USER_1032);
        }
        Integer loginType = tokenUser.getLoginType();
        // 校验redis是否存在
        String tokeyKey = String.format(RedisConstants.CLIENT_TOKEN_USER_KEY,LoginType.getMsgByCode(loginType)) + tokenUser.getId();
        Object o = redisTemplate.opsForValue().get(tokeyKey);
        if(o == null) {
            throw new BusinessException(SystemError.USER_1032);
        } else {
            // 注释判断token是否一致
            /*String redisToken = (String) o;
            // 校验token是否和redis中的一致
            if(!token.equals(redisToken)) {
                throw new BusinessException(SystemError.USER_1032);
            }

            //根据loginType续期token
            if(LoginType.APP.getCode() == loginType) {
                redisTemplate.expire(tokeyKey, tokenTimeoutApp, TimeUnit.DAYS);
            } else if (LoginType.PC.getCode() == loginType) {
                redisTemplate.expire(tokeyKey, 7, TimeUnit.DAYS);
            }*/

        }



        return tokenUser;
    }

    @Override
    public TokenUser getCurrentUser() {
        return super.getCurrentUser();
    }

    @Override
    public Long getCurrentUserId() {
        return super.getCurrentUserId();
    }
}
